<?php
namespace Tools\Controller;
use Common\Controller\AdminbaseController;

/**
 * 合作伙伴后台
 */
class AdminPartnerController extends AdminbaseController{
	
	protected $list_model;
	protected $cate_model;
	
	public function _initialize() {
		parent::_initialize();
		$this->list_model = D("ToolsPartner");
		$this->cate_model = D("ToolsPartnerCate");
	}
	
	// 合作单位列表
	public function index(){

		//分类
		$cate = $this->cate_model
			->order('orderby asc')
			->select();
		$this->assign('cate',$cate);

		//条件-名称
		$partner_name = I('partner_name');
		if($partner_name){
			$where['partner_name'] = array('like',"%$partner_name%");
		}

		//条件-分类
		$partner_cid = I('partner_cid');
		if($partner_cid) {
			$where['partner_cid'] = $partner_cid;
		}
		$this->assign('partner_cid',$partner_cid);	//分类选中值

		//数量
		$count=$this->list_model->where($where)->count();

		//分页
		$page = $this->page($count, 10);

		$db_prefix=C('DB_PREFIX');
		$lists = $this->list_model
			-> alias('a')
			-> field('a.*,b.cate_name')
			-> join($db_prefix.'tools_partner_cate  b  ON a.partner_cid = b.cate_id')
			-> where($where)
			-> order('a.orderby asc')
			-> limit($page->firstRow, $page->listRows)
			-> select();

		$this -> assign("page", $page->show('Admin'));
		$this -> assign('lists',$lists);
		$this->display('index');
	}

	//填加
	public function add() {
		$this -> _modify();
	}

	//修改
	public function edit() {
		$this -> _modify();
	}


	private function _modify() {

		$partner_id = I('partner_id');
		
		if (IS_POST) {
			$validate = array(
				array('partner_name', 'require', '合作伙伴名称不能为空'),
				array('partner_pic', 'require', '合作伙伴的图片不能为空'),
				array('partner_url', 'require', '合作伙伴的链接不能为空'),
			);
			$result = array('status' => TRUE, 'info' => '', 'url' => '');
			
			$this->list_model -> field('partner_cid,partner_name,partner_pic,partner_url,orderby');
			
			if ($this->list_model -> validate($validate) -> create()) {
				if (!$partner_id) {
					$insert_id = $this->list_model -> add();
					if ($insert_id) {
						$result['info'] = '添加成功';
					} else {
						$result['status'] = FALSE;
						$result['info'] = '添加失败';
					}
				} else {
					if($this->list_model -> where(array('partner_id' => $partner_id)) -> save()) {
						$result['info'] = '保存成功';
					} else {
						$result['status'] = FALSE;
						$result['info'] = '保存失败';
					}
				}
			} else {
				//返回错误提示
				$result['status'] = FALSE;
				$result['info'] = $this->list_model -> getError();
			}
			
			if ($result['status']) {
				$this -> success($result['info'],U('AdminPartner/index'));
			} else {
				$this -> error($result['info']);
			}
		}
		
		//单个值
		$partner = $partner_id ? $this->list_model -> find($partner_id) : array();
		$this -> assign('partner', $partner);

		//分类
		$select_cates = $this->cate_model -> select();
		$this -> assign('select_cates', $select_cates);

		//模板
		$this -> display('AdminPartner/form');
	}
	

	//删除
	public function delete() {
		$partner_id = I('partner_id');
		$result = $this->list_model-> delete($partner_id);	
		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$partner_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);
	}


}